(function ($) {
	const user = app.getState();
	var keyDic;
	$.init();
	$.plusReady(function () {
	    baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		
		today = getCurrentDeviceDay() ;//'2021-08-11'
		thisYM = today.substring(0,today.length - 3);
		currYM = thisYM;
		var ymDict = getYMDWithStr(today)
		$('#ymDate')[0].innerText = outChinaYMStr2(ymDict.year,ymDict.month)
		$('.myHeader').on('tap', '#changeYM', selectYearMonth);
		
		FindApprovalSetShow()
		
	}
	// 获取表头
	function FindApprovalSetShow() {
		var self = plus.webview.currentWebview();
		var arg = {
			APPLoginID: user.loginID,
			APPPerID: user.loginPerID,
			parr: self.rw+'^'+'Y'
		}
		// 获取审批设置
		var method = 'web.INMSetComm:FindApprovalSetShow'
		paramDict = {
			'method': method,
			'params': JSON.stringify(arg),
			'type': 'RecQuery'
		}
		// console.log(JSON.stringify(arg));
		$.DHCWebService(paramDict, function(result) {
			var dataArr = result['data'];
			keyDic = dataArr;
			if(dataArr.length > 0){
				dataArr = jsonSort(dataArr,'ShowSort'); //按照后台order字段排序				
				getApprovalDetailByUser(self.keyID)
			}
		}, function(errorStr) {
			console.log(errorStr)
		});
	}
	function getApprovalDetailByUser(keyID){
		var self = plus.webview.currentWebview();
		var arg = {
			loginID: user.loginID,
			APPLoginID: user.loginID,
			APPPerID: user.loginPerID,
			keyID: self.keyID
		}
		// console.log(JSON.stringify(arg));
		// 获取详情
		var method = 'web.INMPlatform:FindApprovalDetailByUser'
		paramDict = {
			'method': method,
			'params': JSON.stringify(arg),
			'type': 'RecQuery'
		}
		$.DHCWebService(paramDict, function(result) {
			var dataArr = result['data'];
			if(dataArr.length > 0){
				var examTableView = document.querySelector('.examWrapper .mui-table-view');
				var html = ''
				var oneCard = ''
				for (var j = 0; j < dataArr.length; j++) {
					console.log(JSON.stringify(dataArr[j]))
					var inner = ''
					for (var i = 0; i < keyDic.length; i++) {
						var one = keyDic[i];
						var key = one.rwProp
						var content = '<div class="key" data-rwProp="'+ one.rwProp +'">'+ one.ShowDesc +'：</div><div class="value">'+dataArr[j][key]+'</div>'
						inner += content;
					}
					var cardBody1 = '<li class="mui-table-view-cell"><div class="content">'
					var cardBody2 = '</div><div class="agreeBtn agree" data-arg="' +dataArr[j]['ToValue']+ '">通过</div><div class="agreeBtn disagree" data-arg="' +dataArr[j]['BackToValue'] + '">驳回</div></li>';
					oneCard = cardBody1 + inner + cardBody2;//一个卡片的内容
					html += oneCard //多个卡片的内容
				}
				examTableView.innerHTML += html;
				// 驳回
				$('.examWrapper .mui-table-view-cell').on('tap', '.disagree', function(e) {
					requestSaveApprovalData(this.getAttribute('data-arg'),this.innerText)
				})
				
				// 通过
				$('.examWrapper .mui-table-view-cell').on('tap', '.agree', function(e) {
					requestSaveApprovalData(this.getAttribute('data-arg'),this.innerText)
				})
				
				$('.examWrapper .mui-table-view-cell').on('tap', '.content', function(e) {
					
				})
			}
		}, function(errorStr) {
			console.log(errorStr)
		});
	}
	function requestSaveApprovalData(value,desc){
		console.log(value,desc)
		var self = plus.webview.currentWebview();
		var arg = {
			loginID: user.loginID,
			APPLoginID: user.loginID,
			APPPerID: user.loginPerID,
			keyID: self.keyID,
			toValue:value,
			toValueDesc:desc,
			option:'',
			rws:self.rw
		}
		// console.log(JSON.stringify(arg));
		// 审批接口
		var method = 'web.INMPlatform:SaveApprovalData'
		paramDict = {
			'method': method,
			'params': JSON.stringify(arg),
			'type': 'Method'
		}
		$.DHCWebService(paramDict, function(result) {
			console.log(JSON.stringify(result))
			if(result.msgcode == '1'){
				mui.toast(result.msg)
			}
		}, function(errorStr) {
			console.log(errorStr)
		});
	}
	
	/*
	 * 选择年月
	 */
	function selectYearMonth() {
		var _self = this;
		if(_self.picker) {
			_self.picker.show(function(rs) {
				var dateStr = rs.text;
				_self.innerText = dateStr.substring(2, dateStr.length);
				_self.picker.dispose();
				_self.picker = null;
			});
		} else {
			var optionsJson = this.getAttribute('data-options') || '{}';
			var options = JSON.parse(optionsJson);
			_self.picker = new mui.DtPicker(options);
			_self.picker.show(function(rs) {
				var dateStr = rs.text;
				_self.innerText = dateStr.substring(2, dateStr.length);
				_self.picker.dispose();
				_self.picker = null;
			});
		}
	}
	
	///刷新
	function refreshMainPage(index){
		
	}
})(mui);